package Beans;

import java.io.Serializable;
import java.text.DecimalFormat;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.validation.constraints.Min;

/**
 *
 * @author rtn
 */
@SessionScoped
@ManagedBean(name = "calc")
public class CalcBean implements Serializable, Converter {

    private double startvol = 20.0;
    private double sg = 1.040;
    @Min(value = 0, message = "miminini")
    private double vann = 0;
    private double totalvol;
    private String fsg;
    private DecimalFormat df = new DecimalFormat("#.000");

    public String getFsg() {
        return fsg;
    }

    public void setFsg(String fsg) {
        this.fsg = fsg;
    }

    public String getSgformatted() {
        return df.format(sg);
    }

    public double getSg() {
        return sg;
    }

    public void setSg(double sg) {
        this.sg = sg;
    }

    public double getStartvol() {
        return startvol;
    }

    public void setStartvol(double startvol) {
        this.startvol = startvol;
    }

    public double getTotalvol() {
        return totalvol;
    }

    public void setTotalvol(double totalvol) {
        this.totalvol = totalvol;
    }

    public double getVann() {
        return vann;
    }

    public void setVann(double vann) {
        this.vann = vann;
    }

    public double totVol() {
        return startvol + vann;
    }

    public String beregnDil() {
        double calc = ((((startvol * ((sg * 1000) - 1000)) / (getStartvol() + getVann())) + 1000) / 1000);
        return df.format(calc);
    }

   /** public double validerTall(FacesContext context, UIComponent component, Object value) {
        if (context == null || component == null) {
            throw new NullPointerException();
        }

        if (value == null) {
            return 0.0;
        }

        if (value instanceof Double) {
            return (Double) value;
        }

        try {
            return (Double.parseDouble(value.toString()));

        } catch (NumberFormatException nfe) {
            throw new ConverterException(MessageFactory.getMessage(context, javax.faces.converter.DoubleConverter.DOUBLE, value, "1999999",
                    MessageFactory.getLabel(context, component)), nfe);
            
         catch (Exception ex) {
            throw new ConverterException(MessageFactory.get)
        }

    }*/

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
/**
 * 147 148 try { 149 return (Double.toString(((Number) value).doubleValue()));
 * 150 } catch (Exception e) { 151 throw new
 * ConverterException(MessageFactory.getMessage( 152 context, STRING_ID, value,
 * 153 MessageFactory.getLabel(context, component)), e);
 *http://www.mkyong.com/jsf2/jsf-2-validateregex-example/
 * http://johnderinger.wordpress.com/2007/10/11/validators-and-converters/
 * http://code.google.com/p/sys-med-prosj/source/browse/trunk/smp2012/web/admin/leggTilKurs.xhtml
 * 
 *
 */

    //http://www.docjar.com/html/api/javax/faces/convert/DoubleConverter.java.html
